/**
 * 
 */
package com.alkandros.calculator.ui.calculator;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.alkandros.calculator.R;
import com.alkandros.calculator.manager.Calculator;
import com.alkandros.calculator.manager.SelectorManager;
import com.alkandros.calculator.manager.Utils;
import com.alkandros.calculator.validator.Form;
import com.alkandros.calculator.validator.NotEmptyValidator;
import com.alkandros.calculator.validator.Validate;

/**
 * @author Jiju Induchoodan
 *
 */
public class CalculatorFragment extends Fragment implements OnClickListener {

	View rootView;
	ActionBar actionBar;
	private Activity context;
	private Spinner spinnerMaterial;
	private Button btnRectangle;
	private Button btnCircle;
	private Button btnTriangle;
	private Button btnCalculate;
	
	private TextView txtQuestionTwo;
	private TextView txtQuestionThree;
	private TextView txtQuestionFour;
	
	private EditText edtAnsTwo;
	private EditText edtAnsThree;
	private EditText edtAnsFour;
	private ArrayAdapter<CharSequence> adapter;
	private int surfaceType;  // 1- Rectangle 2- Circle  3- Triangle
	private float materialArray[] = {300.751f,300.751f,239.5209f,300.751f,239.5209f,239.5209f,239.5209f,239.5209f,239.5209f,300.751f};
	private int spinnerSelectedPos=0;
	Form mForm ;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
	{
		  rootView = inflater.inflate(R.layout.fragment_calculator, container, false);
		  context= getActivity();
		  initialiseUI();
		  setListeners();
		  validate();
		  return rootView;
	}
	
	@SuppressWarnings("deprecation")
	private void initialiseUI()
	{
		spinnerMaterial		= (Spinner)rootView.findViewById(R.id.spn_type);
		btnRectangle   		= (Button)rootView.findViewById(R.id.btn_rectangular);
		btnCircle	   		= (Button)rootView.findViewById(R.id.btn_circle);
		btnTriangle	   		= (Button)rootView.findViewById(R.id.btn_triangle);
		btnCalculate	   	= (Button)rootView.findViewById(R.id.btn_calculate);
		
		txtQuestionTwo 		= (TextView)rootView.findViewById(R.id.txt_questiontwo);
		txtQuestionThree 	= (TextView)rootView.findViewById(R.id.txt_questionthree);
		txtQuestionFour 	= (TextView)rootView.findViewById(R.id.txt_questionfour);
		
		edtAnsTwo	 		= (EditText)rootView.findViewById(R.id.edt_anstwo);
		edtAnsThree	 		= (EditText)rootView.findViewById(R.id.edt_ansthree);
		edtAnsFour	 		= (EditText)rootView.findViewById(R.id.edt_ansfour);
		
		adapter=ArrayAdapter.createFromResource(context.getApplicationContext(), R.array.material_array, R.layout.spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerMaterial.setAdapter(adapter);
		surfaceType=1;
		spinnerMaterial.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) 
			{
				spinnerSelectedPos =pos;
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) 
			{
				
			}
		});
		btnCalculate.setBackgroundDrawable(SelectorManager.
				getButtonDrawableByScreenCathegory(context, R.drawable.button_bg, R.drawable.button_press));
		
		btnRectangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_press_color)));
		btnCircle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
		btnTriangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
	}
	
	private void validate()
	{
		Validate field2 = new Validate(edtAnsTwo);
		Validate field3 = new Validate(edtAnsThree);
		Validate field4 = new Validate(edtAnsFour);
		
		field2.addValidator(new NotEmptyValidator(context));
		field3.addValidator(new NotEmptyValidator(context));
		field4.addValidator(new NotEmptyValidator(context));
		
		mForm = new Form();
		mForm.addValidates(field2);
		mForm.addValidates(field3);
		mForm.addValidates(field4);
	}
	private void setListeners()
	{
		btnCalculate.setOnClickListener(this);
		btnRectangle.setOnClickListener(this);
		btnCircle.setOnClickListener(this);
		btnTriangle.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) 
	{
		float res;
		if(v==btnCalculate)
		{
			if(mForm.validate())
			{
				if(surfaceType==1)
				{
					res=Calculator.calculate(Integer.valueOf(edtAnsTwo.getText().toString()), 
							Integer.valueOf(edtAnsThree.getText().toString()), 
							Integer.valueOf(edtAnsFour.getText().toString()),
							materialArray[spinnerSelectedPos],
							surfaceType);
				}
				else if(surfaceType==2)
				{
					int diameter = Integer.valueOf(edtAnsTwo.getText().toString());
					int radius   = diameter/2;
					res=Calculator.calculate(radius, 
							1,Integer.valueOf(edtAnsFour.getText().toString()), 
							materialArray[spinnerSelectedPos],
							surfaceType);
				}
				else
				{
					res=Calculator.calculate(Integer.valueOf(edtAnsTwo.getText().toString()), 
							Integer.valueOf(edtAnsThree.getText().toString()), 
							Integer.valueOf(edtAnsFour.getText().toString()),
							materialArray[spinnerSelectedPos],
							surfaceType);
				}
				Log.e("", "Result"+ res);
				showResult(res);
			}
			else
			{
				
			}
			
		}
		else if(v==btnRectangle)
		{
			changeQuestions(1);
		}
		else if(v==btnCircle)
		{
			changeQuestions(2);
		}
		else if(v==btnTriangle)
		{
			changeQuestions(3);
		}
	}
	private void changeQuestions(int type)
	{
		switch (type)
		{
			
			case 1:	txtQuestionThree.setVisibility(View.VISIBLE);
					edtAnsThree.setVisibility(View.VISIBLE);
					txtQuestionTwo.setText(getString(R.string.question_two_rectangular));
			 		txtQuestionThree.setText(getString(R.string.question_three_rectangular));
			 		txtQuestionFour.setText(getString(R.string.question_four_rectangular));
			 		surfaceType=1;
			 		 edtAnsTwo.setText("");
					 edtAnsThree.setText("");
					 edtAnsFour.setText("");
					 btnRectangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_press_color)));
					 btnCircle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
					 btnTriangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
					break;
			case 2:  txtQuestionTwo.setText(getString(R.string.question_two_circle));
					 txtQuestionFour.setText(getString(R.string.question_three_circle));
					 txtQuestionThree.setVisibility(View.GONE);
					 edtAnsThree.setVisibility(View.GONE);
					 surfaceType=2;
					 edtAnsTwo.setText("");
					 edtAnsThree.setText("1");
					 edtAnsFour.setText("");
					 btnRectangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
					 btnCircle.setBackgroundColor(Color.parseColor(getString(R.string.tab_press_color)));
					 btnTriangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
					break;
			case 3: txtQuestionTwo.setText(getString(R.string.question_two_triange));
			 		txtQuestionThree.setText(getString(R.string.question_three_triange));
			 		txtQuestionFour.setText(getString(R.string.question_four_triange));
			 		txtQuestionThree.setVisibility(View.VISIBLE);
			 		edtAnsThree.setVisibility(View.VISIBLE);
					surfaceType=3;
					edtAnsTwo.setText("");
					 edtAnsThree.setText("");
					 edtAnsFour.setText("");
					 btnRectangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
					 btnCircle.setBackgroundColor(Color.parseColor(getString(R.string.tab_normal_color)));
					 btnTriangle.setBackgroundColor(Color.parseColor(getString(R.string.tab_press_color)));
					break;
			default: 
					break;
		}
	}
	@SuppressWarnings("deprecation")
	private void showResult(float result)
	{
		edtAnsFour.setText("");
		edtAnsThree.setText("");
		edtAnsTwo.setText("");
		final Dialog dlg = new Dialog(context, R.style.IconsDialog);
		dlg.setContentView(R.layout.result_view);
		TextView txtResult = (TextView) dlg.findViewById(R.id.txt_result);
		ImageView imgClose = (ImageView) dlg.findViewById(R.id.img_close);
		txtResult.setText(String.valueOf(result));
		Utils.setBackground(context,
				imgClose,SelectorManager.
				getButtonDrawableByScreenCathegory(context,
				R.drawable.close_normal,
				R.drawable.close_press));
		dlg.show();
		imgClose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dlg.cancel();
			}
		});
	}
}